<?php


namespace app\api\controller;


use app\api\library\withdraw\delete;
use app\api\library\withdraw\withdraw_list;
use app\api\library\withdraw\withdrawType;
use app\api\library\withdraw\currentWithdrawInfo;
use app\api\library\withdraw\getUserCurrentWithdrawInfo;
use app\api\library\withdraw\withdraw as withdrawDetail;
use app\common\controller\IndexController;
use think\Exception;
use think\facade\Db;
use think\facade\Filesystem;

class Withdraw extends IndexController
{
    /**
     * 添加
     */
    public function add()
    {
        $userId  = _checkEmptyParam($this->request->post('ui'), '用户不能为空');
        $type    = _checkEmptyParam($this->request->post('te'), '收款方式不能为空');
        $name    = _checkEmptyParam($this->request->post('ne'), '姓名不能为空');
        $account = $this->request->post('aat');
        $image   = $this->request->file('ie');
        if ($type == 3) {
            _checkEmptyParam($account, '支付宝账号不能为空');
        } else {
            _checkEmptyParam($image, '收款码不能为空');
        }
        if ( !empty($image)) {
            $data         = [
                'upload_type' => $this->request->post('upload_type'),
                'file'        => $image,
            ];
            $uploadConfig = sysconfig('upload');
            empty($data[ 'upload_type' ]) && $data[ 'upload_type' ] = $uploadConfig[ 'upload_type' ];
            $rule = [
                'upload_type|指定上传类型有误' => "in:{$uploadConfig['upload_allow_type']}",
                'file|文件'              => "require|file|fileExt:{$uploadConfig['upload_allow_ext']}|fileSize:{$uploadConfig['upload_allow_size']}",
            ];
            $this->validate($data, $rule);
            try {
                $upload          = Filesystem::disk('public')->putFile('userUpload', $data[ 'file' ]);
                $completeFileUrl = request()->domain() . '/' . str_replace(DIRECTORY_SEPARATOR, '/', $upload);
            } catch (\Exception $e) {
                $this->error($e->getMessage());
            }
        }
        $addData = [
            'user_id'     => $userId,
            'name'        => $name,
            'type'        => $type,
            'account'     => $account,
            'create_time' => time(),
            'update_time' => time(),
        ];
        if (isset($upload)) {
            $addData[ 'image' ] = $completeFileUrl;
        }
        try {
            //开启事务
            Db::startTrans();
            if (false === Db::name('mall_withdraw_info')->insert($addData)) {
                _throwNewException('添加');
            }
            // 提交事务
            Db::commit();
        } catch (Exception $e) {
            // 回滚事务
            Db::rollback();
            _outPut(204, '', $e->getMessage());
        }
        _outPut(200);
    }
    /**
     * 修改
     */
    public function update()
    {
        $id      = _checkEmptyParam($this->request->post('id'), 'ID不能为空');
        $userId  = _checkEmptyParam($this->request->post('ui'), '用户不能为空');
        $type    = _checkEmptyParam($this->request->post('te'), '收款方式不能为空');
        $name    = _checkEmptyParam($this->request->post('ne'), '姓名不能为空');
        $account = $this->request->post('aat');
        $image   = $this->request->file('ie');
        if ($type == 3) {
            _checkEmptyParam($account, '支付宝账号不能为空');
        } else {
            _checkEmptyParam($image, '收款码不能为空');
        }
        if ( !empty($image)) {
            $data         = [
                'upload_type' => $this->request->post('upload_type'),
                'file'        => $image,
            ];
            $uploadConfig = sysconfig('upload');
            empty($data[ 'upload_type' ]) && $data[ 'upload_type' ] = $uploadConfig[ 'upload_type' ];
            $rule = [
                'upload_type|指定上传类型有误' => "in:{$uploadConfig['upload_allow_type']}",
                'file|文件'              => "require|file|fileExt:{$uploadConfig['upload_allow_ext']}|fileSize:{$uploadConfig['upload_allow_size']}",
            ];
            $this->validate($data, $rule);
            try {
                $upload          = Filesystem::disk('public')->putFile('userUpload', $data[ 'file' ]);
                $completeFileUrl = request()->domain() . '/' . str_replace(DIRECTORY_SEPARATOR, '/', $upload);
            } catch (\Exception $e) {
                $this->error($e->getMessage());
            }
        }
        $updateData = [
            'user_id'     => $userId,
            'name'        => $name,
            'type'        => $type,
            'account'     => $account,
            'create_time' => time(),
            'update_time' => time(),
        ];
        if (isset($upload)) {
            $updateData[ 'image' ] = $completeFileUrl;
        }
        try {
            //开启事务
            Db::startTrans();
            if (false === Db::name('mall_withdraw_info')->where('id', $id)->update($updateData)) {
                _throwNewException('添加');
            }
            // 提交事务
            Db::commit();
        } catch (Exception $e) {
            // 回滚事务
            Db::rollback();
            _outPut(204, '', $e->getMessage());
        }
        _outPut(200);
    }
    /**
     * 删除用户地址信息
     */
    public function delete()
    {
        $req = $this->run(WEB_KEY);
//        $req = request()->param();
        //处理参数
        $display = [
            ['id', 'id', 'int'],
            ['userId', 'ui', 'int'],
        ];
        //校验参数
        $param = _displayParam($req, $display);
        delete\V::run($param);
    }
    /**
     * 获取用户提现方式信息
     */
    public function withdrawList()
    {
        $req = $this->run(WEB_KEY);
//        $req = request()->param();
        //处理参数
        $display = [
            ['userId', 'ui', 'int'],
        ];
        //校验参数
        $param = _displayParam($req, $display);
        
        
        withdraw_list\V::run($param);
    }
    /**
     * 获取提现方式
     */
    public function withdrawType()
    {
        $req = $this->run(WEB_KEY);
//        $req = request()->param();
        //处理参数
        $display = [
            ['userId', 'ui', 'int'],
        ];
        //校验参数
        $param = _displayParam($req, $display);
        
        
        withdrawType\V::run($param);
    }
    
    /**
     * 设置默认提现方式
     */
    public function currentWithdrawInfo()
    {
        $req = $this->run(WEB_KEY);
//        $req = request()->param();
        //处理参数
        $display = [
            ['id', 'id', 'int'],
            ['userId', 'ui', 'int'],
        ];
        //校验参数
        $param = _displayParam($req, $display);
        currentWithdrawInfo\V::run($param);
    }
    
    /**
     * 获取用户默认提现方式
     */
    public function getUserCurrentWithdrawInfo()
    {
        $req = $this->run(WEB_KEY);
//        $req = request()->param();
        //处理参数
        $display = [
            ['userId', 'ui', 'int'],
        ];
        //校验参数
        $param = _displayParam($req, $display);
        
        
        getUserCurrentWithdrawInfo\V::run($param);
    }
    
    /**
     * 提现申请
     */
    public function withdraw()
    {
        $req = $this->run(WEB_KEY);
//        $req = request()->param();
        //处理参数
        $display = [
            ['withdrawId', 'wi', 'int'],
            ['userId', 'ui', 'int'],
            ['account', 'at', 'int'],
            ['code', 'ce', 'int'],
        ];
        //校验参数
        $param = _displayParam($req, $display);
        withdrawDetail\V::run($param);
        
    }
}